class ProductsController < ApplicationController
  # GET /products
  # GET /products.xml
  def index
    @title='产品库存列表'
    unless params[:f_store_id].blank?
      @store_title = Store.find(params[:f_store_id]).name
    else
      @store_title = "汇总"
    end
    
    if (params[:f_store_id] != nil) && (params[:f_store_id].to_i != 4)
      top3_store_get_prouducts
    elsif(params[:f_store_id].to_i == 4)
       forth_store_get_prouducts
    else
      @products = Product.find(:all,:include => 'category',:order =>'categories.position')
    end
         
    @products_group=@products.group_by &:category_id
  end
  
  def list
    
    f_store_id = params[:f_store_id].to_i
    products_array = Inproduct.all(:conditions => {:f_store_id => f_store_id}).map{|p| {p.product_id => p.amount}}
    product_ids = count_products_by_store(products_array)
    
    @products = Product.find(product_ids,:include => 'category',:order =>'categories.position')
    @products_group=@products.group_by &:category_id
    
    render "index"
    
  end
  

  # GET /products/1
  # GET /products/1.xml
  def show
    @product = Product.find(params[:id])
    @pu=@product.productusers(:order => "updated_at desc")
    @pu_out_sum=@pu.sum(:amount)
    @pu_out_cost=@pu.sum(:total_cost)
    # render :update do |page|
    #   page.replace_html 'users_list',:partial=>'users_list'
    # end

  # respond_to do |format|
  #   format.html # show.html.erb
  #   format.xml  { render :xml => @product }
  # end
  end

  # GET /products/new
  # GET /products/new.xml
  def new
    @product = Product.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @product }
    end
  end

  # GET /products/1/edit
  def edit
    @product = Product.find(params[:id])
  end

  # POST /products
  # POST /products.xml
  def create
    @product = Product.new(params[:product])
    

    respond_to do |format|
      if  @product.save
        flash[:notice] = 'Product was successfully created.'
        format.html { redirect_to(products_path) }
        format.xml  { render :xml => @product, :status => :created, :location => @product }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @product.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /products/1
  # PUT /products/1.xml
  def update
    @product = Product.find(params[:id])

    respond_to do |format|
      if @product.update_attributes(params[:product])
        flash[:notice] = 'Product was successfully updated.'
        format.html { redirect_to(products_path) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @product.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /products/1
  # DELETE /products/1.xml
  def destroy
    @product = Product.find(params[:id])
    @product.destroy
  end
  
  def in_p
     @title='产品入库'
     @products = Product.find(:all,:include => 'category',:order => 'category_id')
     @products_group=@products.group_by &:category_id
   end
   
  def in_p_list
    @title="入库列表"
    if (params[:f_store_id] != nil && params[:f_store_id].to_i != 4)
      @inproducts = Inproduct.find(:all,:conditions => {:f_store_id => params[:f_store_id].to_i,:l_store_id => nil} ,:include => 'product',:order => "products.category_id,inproducts.created_at desc")
    elsif(params[:f_store_id].to_i == 4)
      @inproducts = Inproduct.find(:all,:conditions => ["f_store_id = 4 or l_store_id = 4"],:include => 'product',:order => "products.category_id,inproducts.created_at desc")
    end
    @inproducts_group = @inproducts.group_by {|ip| ip.created_at.beginning_of_day }
  end
  
  def out_p_list
    @title="出库列表"
    @inproducts = Inproduct.find(:all,:conditions => {:f_store_id => params[:f_store_id].to_i,:l_store_id => 4} ,:include => 'product',:order => "products.category_id,inproducts.created_at desc")
    @inproducts_group = @inproducts.group_by {|ip| ip.created_at.beginning_of_day }
  end
  
      def out_p
     @title='出库'
      f_store_id = params[:f_store_id].to_i
      products_array = Inproduct.all(:conditions => {:f_store_id => f_store_id}).map{|p| {p.product_id => p.amount}}
      product_ids = count_products_by_store(products_array).keys
     @products = Product.find(:all,:include => 'category',:order => 'category_id')
     @products_group=@products.group_by &:category_id
   end
   
  # def out_p_list
  #   @title="出库列表"
  #   @inproducts = Inproduct.find(:all,:conditions => {:f_store_id => params[:f_store_id].to_i} ,:include => 'product',:order => "products.category_id,inproducts.created_at desc")
  #   @inproducts_group = @inproducts.group_by {|ip| ip.created_at.beginning_of_day }
  # end
  
  
  private
    def count_products_by_store(source_array)
      result = {}
      source_array.each do |h|
        key = h.keys.first
        value = h.values.first
        unless result.has_key?(key)
          result[key] = value
        else
          result[key] = result[key] + value
        end
      end
      return result
    end

    def top3_store_get_prouducts
      f_store_id = params[:f_store_id].to_i
      @products_array = Inproduct.all(:conditions => {:f_store_id => f_store_id}).map{|p| {p.product_id => p.amount}}
      @products_size_array = count_products_by_store(@products_array)
      product_ids = @products_size_array.keys
      # logger.info "*" * 50 
      # logger.info @products_array.inspect
      # logger.info "*" * 50 
      # logger.info @products_size_array.inspect
      @products = Product.find(product_ids,:include => 'category',:order =>'categories.position')
    end
    
    def forth_store_get_prouducts
       @products_array = Inproduct.all(:conditions => ["f_store_id = 4 or l_store_id = 4"]).map{|p| {p.product_id => p.amount}}
       @products_size_array = count_products_by_store(@products_array)
       product_ids = @products_size_array.keys
       # logger.info "*" * 50 
       # logger.info @products_array.inspect
       # logger.info "*" * 50 
       # logger.info @products_size_array.inspect
       @products = Product.find(product_ids,:include => 'category',:order =>'categories.position')
    end
  
end
